home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / InvisibleButton.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  5.4 KB  |  153 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Event;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9. import java.awt.Rectangle;
  10.  
  11. //    01/02/97    RKM    Changed paintComponent to not cause recursive drawing on overlap
  12. //    01/02/97    RKM    Changed paintComponent to flush image after it is used
  13. //    01/16/97    RKM    Added override of update to avoid flicker
  14. //    01/18/07    RKM    Added more documentation and checked return from img.getGraphics
  15. //    01/29/97    TWB    Integrated changes from Windows and RKM's changes
  16. //     01/29/97    TWB    Integrated changes from Macintosh
  17. //     02/01/97    RKM    Extracted transparency drawing trick into TransparencyTrickUtils
  18.  
  19. /**
  20.  * Use this component to create an invisible area, usually within an image, 
  21.  * that initiates an action when clicked. Specifically, use InvisibleButton to
  22.  * <UL>
  23.  * <DT>╖ create a "hot spot" on an image or on a component</DT>
  24.  * <DT>╖ accept or yield focus</DT>
  25.  * <DT>╖ respond to a user event</DT>
  26.  * <DT>╖ send an action event to another component</DT>
  27.  * </UL>
  28.  * Button tips:
  29.  * <UL>
  30.  * <DT>╖ Buttons accept and yield focus automatically by default.</DT>
  31.  * <DT>╖ Buttons accept clicked events automatically by default.</DT>
  32.  * <DT>╖ To send an action event to another component, use the Interaction 
  33.  * Wizard. Optionally, you can override the InvisibleButtonÆs click event in 
  34.  * project source code.</DT>
  35.  * <DT>╖ Overlapping components - browsers handle components layered on each 
  36.  * other differently. Some browsers display/layer the InvisibleButtons on top
  37.  * of a particular component, while others display them underneath. Therefore,
  38.  * it often requires two sets of InvisibleButtons to ensure that one is "on top",
  39.  * one on top of the particular component and the other underneath. 
  40.  * </UL>
  41.  * Implements the Runnable interface to support threads.
  42.  * <p>
  43.  *
  44.  */
  45. public class InvisibleButton
  46.     extends Canvas
  47.     implements TransparencyTrick
  48. {
  49.     /**
  50.      * True if the button is currently pressed.
  51.      */
  52.     protected boolean pressed;
  53.     
  54.     /**
  55.      * Constructs an InvisibleButton.
  56.      */
  57.     public InvisibleButton() {
  58.         pressed = false;
  59.     }
  60.     
  61.     /**
  62.      * Handles redrawing of this component on the screen.
  63.      * This is a standard Java AWT method which gets called by the Java
  64.      * AWT (repaint()) to handle repainting this component on the screen.
  65.      * The graphics context clipping region is set to the bounding rectangle
  66.      * of this component and its <0,0> coordinate is this component's 
  67.      * top-left corner.
  68.      * Typically this method paints the background color to clear the
  69.      * component's drawing space, sets graphics context to be the foreground
  70.      * color, and then calls paint() to draw the component.
  71.      *
  72.      * It is overridden here to prevent the flicker associated with the standard 
  73.      * update() method's repainting of the background before painting the component
  74.      * itself.
  75.      *
  76.      * @param g the graphics context
  77.      * @see java.awt.Component#repaint
  78.      * @see #paint
  79.      */
  80.     public void update(Graphics g)
  81.     {
  82.         paint(g);
  83.     }
  84.     
  85.     /**
  86.      * Paints this component using the given graphics context.
  87.      * This is a standard Java AWT method which typically gets called
  88.      * by the AWT to handle painting this component. It paints this component
  89.      * using the given graphics context. The graphics context clipping region
  90.      * is set to the bounding rectangle of this component and its <0,0>
  91.      * coordinate is this component's top-left corner.
  92.      * It paints what is under the button, so as to appear invisible.
  93.      * @param g the graphics context used for painting
  94.      * @see java.awt.Component#repaint
  95.      * @see #update
  96.      */
  97.     public void paint(Graphics g) {
  98.         TransparencyTrickUtils.paintComponentsBehind(this, g);
  99.     }
  100.     
  101.     /**
  102.      * Processes MOUSE_DOWN events.
  103.      * This is a standard Java AWT method which gets called by the AWT
  104.      * method handleEvent() in response to receiving a MOUSE_DOWN
  105.      * event. These events occur when the mouse button is pressed while
  106.      * inside this component.
  107.      * 
  108.      * @param e the event
  109.      * @param x the component-relative horizontal coordinate of the mouse
  110.      * @param y the component-relative vertical coordinate of the mouse
  111.      * 
  112.      * @return always true since the event was handled
  113.      * 
  114.      * @see #mouseUp
  115.      * @see java.awt.Component#handleEvent
  116.      */
  117.     public boolean mouseDown(Event e, int x, int y)
  118.     {
  119.         pressed = true;
  120.     
  121.         return true;
  122.     }
  123.     
  124.     /**
  125.      * Processes MOUSE_UP events.
  126.      * This is a standard Java AWT method which gets called by the AWT
  127.      * method handleEvent() in response to receiving a MOUSE_UP
  128.      * event. These events occur when the mouse button is released while 
  129.      * inside this component.
  130.      * If the mouse was pressed inside the button then an event is posted.
  131.      * 
  132.      * @param e the event
  133.      * @param x the component-relative horizontal coordinate of the mouse
  134.      * @param y the component-relative vertical coordinate of the mouse
  135.      * 
  136.      * @return always true since the event was handled
  137.      * 
  138.      * @see #mouseDown
  139.      * @see java.awt.Component#handleEvent
  140.      */
  141.     public boolean mouseUp(Event e, int x, int y)
  142.     {
  143.         if (pressed)
  144.         {
  145.             pressed = false;
  146.             postEvent(new Event(this, Event.ACTION_EVENT, null));
  147.         }
  148.     
  149.         return true;
  150.     }
  151. }
  152.  
  153.